package com.dstz.bpm.core.manager.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.api.exception.BusinessMessage;
import com.dstz.base.api.query.QueryFilter;
import com.dstz.base.api.query.QueryOP;
import com.dstz.base.core.util.StringUtil;
import com.dstz.base.manager.impl.BaseManager;
import com.dstz.bpm.api.constant.TaskStatus;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.core.dao.BpmTaskDao;
import com.dstz.bpm.core.manager.BpmInstanceManager;
import com.dstz.bpm.core.manager.BpmTaskManager;
import com.dstz.bpm.core.manager.BpmTaskOpinionManager;
import com.dstz.bpm.core.manager.TaskIdentityLinkManager;
import com.dstz.bpm.core.model.BpmTask;
import com.dstz.bpm.core.model.BpmTaskOpinion;
import com.dstz.bpm.core.model.TaskIdentityLink;
import com.dstz.bpm.engine.model.BpmIdentity;
import com.dstz.org.api.constant.GroupTypeConstant;
import com.dstz.sys.api.model.SysIdentity;
import com.dstz.sys.util.ContextUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service("bpmTaskManager")
public class BpmTaskManagerImpl extends BaseManager<String, BpmTask> implements BpmTaskManager {
   @Resource
   BpmTaskDao o;
   @Resource
   TaskIdentityLinkManager p;
   @Resource
   BpmInstanceManager q;
   @Resource
   BpmTaskOpinionManager j;

   @Override
   public List<BpmTask> getByInstIdNodeId(String instId, String nodeId) {
      return this.o.getByInstIdNodeId(instId, nodeId);
   }

   @Override
   public List<BpmTask> getByInstId(String instId) {
      return this.o.getByInstIdNodeId(instId, (String)null);
   }

   @Override
   public void removeByInstId(String instId) {
      this.o.removeByInstId(instId);
   }

   @Override
   public List<BpmTask> getTodoList(String userId, QueryFilter queryFilter) {
      Set<String> userRights = this.p.getUserRights(userId);
      queryFilter.addParamsFilter("userRights", userRights);
      queryFilter.addParamsFilter("userId", ContextUtil.getCurrentUserId());
      List<BpmTask> taskList = this.o.getTodoList(queryFilter);
      return taskList;
   }

   @Override
   public List getTodoList(QueryFilter queryFilter) {
      String userId = ContextUtil.getCurrentUserId();
      String type = (String)queryFilter.getParams().get("type");
      String title = (String)queryFilter.getParams().get("subject");
      if (StringUtil.isNotEmpty(title)) {
         queryFilter.addFilter("subject_", title, QueryOP.LIKE);
      }

      if ("done".equals(type)) {
         return this.q.getApproveHistoryList(userId, queryFilter);
      } else {
         Set<String> userRights = this.p.getUserRights(userId);
         queryFilter.addParamsFilter("userRights", userRights);
         queryFilter.addParamsFilter("userId", userId);
         return this.o.getTodoList(queryFilter);
      }
   }

   @Override
   public void assigneeTask(String taskId, String userId, String userName) {
      BpmTask task = (BpmTask)this.get(taskId);
      if (task == null) {
         throw new BusinessException("任务可能已经被处理，请刷新。", BpmStatusCode.TASK_NOT_FOUND);
      } else {
         task.setAssigneeId(userId);
         task.setAssigneeNames(userName);
         task.setStatus(TaskStatus.DESIGNATE.getKey());
         this.update(task);
         BpmTaskOpinion taskOpinion = this.j.getByTaskId(taskId);
         taskOpinion.setAssignInfo(String.format("user-%s-%s,", userName, userId));
         this.j.update(taskOpinion);
      }
   }

   public void unLockTask(String taskId) {
      BpmTask task = (BpmTask)this.get(taskId);
      List<TaskIdentityLink> identitys = this.p.getByTaskId(task.getId());
      if (CollectionUtil.isEmpty(identitys)) {
         throw new BusinessMessage("该任务并非多候选人状态，无效的操作！");
      } else {
         StringBuilder nameSb = new StringBuilder();

         TaskIdentityLink identity;
         for(Iterator var5 = identitys.iterator(); var5.hasNext(); nameSb.append(identity.getIdentityName())) {
            identity = (TaskIdentityLink)var5.next();
            if (nameSb.length() > 0) {
               nameSb.append(";");
            }

            if (!"user".equals(identity.getType())) {
               GroupTypeConstant groupTpe = GroupTypeConstant.fromStr(identity.getType());
               if (groupTpe != null) {
                  nameSb.append("[").append(groupTpe.label()).append("]");
               }
            } else {
               nameSb.append("[用户]");
            }
         }

         task.setAssigneeId("0");
         task.setAssigneeNames(nameSb.toString());
         task.setStatus(TaskStatus.NORMAL.getKey());
         this.update(task);
         StringBuilder assignInfo = new StringBuilder();
         if (CollectionUtil.isNotEmpty(identitys)) {
            Iterator var9 = identitys.iterator();

            while(var9.hasNext()) {
               TaskIdentityLink identityTmp = (TaskIdentityLink)var9.next();
               assignInfo.append(identityTmp.getType()).append("-").append(identityTmp.getIdentityName()).append("-").append(identityTmp.getIdentity()).append(",");
            }
         }

         BpmTaskOpinion taskOpinion = this.j.getByTaskId(taskId);
         taskOpinion.setAssignInfo(assignInfo.toString());
         this.j.update(taskOpinion);
      }
   }

   @Override
   public List<SysIdentity> getAssignUserById(BpmTask task) {
      if (StringUtil.isNotZeroEmpty(task.getAssigneeId()) && !"-1".equals(task.getAssigneeId())) {
         return Arrays.asList(new BpmIdentity(task.getAssigneeId(), task.getAssigneeNames(), "user"));
      } else {
         List<SysIdentity> identityList = new ArrayList();
         List<TaskIdentityLink> identitys = this.p.getByTaskId(task.getId());
         Iterator var4 = identitys.iterator();

         while(var4.hasNext()) {
            TaskIdentityLink link = (TaskIdentityLink)var4.next();
            identityList.add(new BpmIdentity(link.getIdentity(), link.getIdentityName(), link.getType()));
         }

         return identityList;
      }
   }

   @Override
   public List<BpmTask> getByParentId(String parentId) {
      return this.o.getByParentId(parentId);
   }
}
